package org.lee.controller;

import java.util.List;

import org.hibernate.validator.constraints.Length;
import org.lee.bean.Person;
import org.lee.bean.Person1;
import org.lee.service.PrimaryService;
import org.lee.service.SecondaryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.joinway.framework.bean.logging.annotation.InputLog;
import com.joinway.framework.core.controller.ExceptionController;

@RestController
@RequestMapping("/")
@Validated
public class SampleController extends ExceptionController {

	private final static Logger log = LoggerFactory.getLogger(SampleController.class);
	
	@Autowired protected PrimaryService service;
	@Autowired protected SecondaryService service2;
	
    @RequestMapping("/testdb/person")
    public List<Person> getAll() throws Exception {
    	log.info("find all person");
    	return service.findAllPerson();
    }

    @RequestMapping("/testdb/add/person")
    @InputLog
    public Person addPerson(@Length(min=2) @RequestParam("name") String name) throws Exception {
    	return service.addPerson(name);
    }

    @RequestMapping("/testdb1/person")
    public List<Person1> getAll2() throws Exception {
    	return service2.findAllPerson();
    }

    @RequestMapping("/testdb1/add/person")
    @InputLog
    public Person1 addPerson2(@RequestParam("name") String name) throws Exception {
    	return service2.addPerson(name);
    }

}